package cn.momoshenchi.medicine.service.impl;

import cn.momoshenchi.medicine.pojo.*;
import cn.momoshenchi.medicine.service.*;
import cn.momoshenchi.medicine.util.AssertUtil;
import cn.momoshenchi.medicine.vo.Drug;
import cn.momoshenchi.medicine.vo.PreDetail;
import cn.momoshenchi.medicine.vo.Prescribe;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.momoshenchi.medicine.mapper.PrescriptionInfoMapper;
import io.goeasy.GoEasy;
import io.goeasy.publish.PublishListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class PrescriptionInfoServiceImpl extends ServiceImpl<PrescriptionInfoMapper, PrescriptionInfo>
        implements PrescriptionInfoService
{

    @Autowired
    private PrescriptionDrugService prescriptionDrugService;

    @Autowired
    private BaseOrganizationService organizationService;

    @Autowired
    private ConsultAskService consultAskService;

    @Autowired
    private BaseDrugService drugService;

    @Autowired
    private GoEasy goEasy;

    @Autowired
    private PublishListener publishListener;

    @Override
    @Transactional(rollbackFor = SQLException.class)
    public void prescribe(List<Prescribe> prescribeList)
    {
        String consultId= prescribeList.get(0).getConsultId();
        String userId =prescribeList.get(0).getUserId();
        prescribeList.forEach(prescribe-> {
            List<Drug> drugList = prescribe.getDrugList();
            PrescriptionInfo prescriptionInfo = new PrescriptionInfo();
            BeanUtils.copyProperties(prescribe, prescriptionInfo);
            prescriptionInfo.setPrescriptionStatus("1");
            AssertUtil.isTrue(!save(prescriptionInfo),"操作失败");

            String prescriptionId = prescriptionInfo.getPrescriptionId();
            List<PrescriptionDrug> prescriptionDrugList = new ArrayList<>();

            for (int i = 0; i < drugList.size(); i++)
            {
                Drug item=drugList.get(i);
                Integer drugId=item.getDrugId();
                BaseDrug baseDrug = drugService.getById(drugId);
                PrescriptionDrug prescriptionDrug = new PrescriptionDrug();

                //注意这里的先后顺序,drug有些字段是空的
                BeanUtils.copyProperties(item, prescriptionDrug);
                BeanUtils.copyProperties(baseDrug,prescriptionDrug);
                prescriptionDrug.setPrescriptionId(prescriptionId);
                prescriptionDrug.setSortNumber(i);
                prescriptionDrugList.add(prescriptionDrug);
            }

            AssertUtil.isTrue(!prescriptionDrugService.saveBatch(prescriptionDrugList),"操作失败");

        });
        boolean res=consultAskService.update(new UpdateWrapper<ConsultAsk>().set("consult_status",Byte.parseByte("3")).set("finish_time",new Date()).eq("consult_id",consultId));
        AssertUtil.isTrue(!res,"操作失败");
        goEasy.publish("user_"+userId, "医生已提交处方", publishListener);
    }

    @Override
    public PreDetail preDetail(String consultId)
    {
        PreDetail preDetail=new PreDetail();
        ConsultAsk consultAsk = consultAskService.getById(consultId);
        AssertUtil.isTrue(consultAsk==null,"复诊Id不存在");
        BeanUtils.copyProperties(consultAsk,preDetail);

        List<Prescribe>prescribeList=new ArrayList<>();
        List<PrescriptionInfo> infoList = list(new QueryWrapper<PrescriptionInfo>().eq("consult_id", consultId));
        for (PrescriptionInfo info : infoList)
        {
            Prescribe prescribe = new Prescribe();
            String orgId=info.getOrgId();

            BeanUtils.copyProperties(info, prescribe);
            //添加orgNme
            BaseOrganization baseOrganization = organizationService.getOne(new QueryWrapper<BaseOrganization>().eq("organization_id", orgId));
            prescribe.setOrgName(baseOrganization.getOrganizationName());
            List<PrescriptionDrug> prescriptionDrugList = prescriptionDrugService.list(new QueryWrapper<PrescriptionDrug>().eq("prescription_id", info.getPrescriptionId()));
            List<Drug> drugList = new ArrayList<>();
            prescriptionDrugList.forEach(item -> {
                Drug drug = new Drug();
                BeanUtils.copyProperties(item, drug);
                drugList.add(drug);
            });
            prescribe.setDrugList(drugList);
            prescribeList.add(prescribe);
        }
        preDetail.setPrescribeList(prescribeList);
        return preDetail;
    }


}




